package com.finstone.data.spider.eshop.processor.weibo;

import com.finstone.data.spider.eshop.UnicodeConverter;
import com.finstone.data.spider.eshop.service.impl.WeiBoPageService;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.util.TextUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Sean on 2016/3/22.
 */
@Component
public class WeiBoPageProcessor implements PageProcessor {

    @Autowired
    private WeiBoPageService eShopGoodsService;

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private Site site = Site.me().setCycleRetryTimes(3).setTimeOut(30000);
    // 文本块正文匹配正则
    private final String blockRegex = "<script>STK\\s&&\\sSTK\\.pageletM\\s&&\\sSTK\\.pageletM\\.view\\(.*\\)";
    private Pattern pattern = Pattern.compile(blockRegex);
    private Document document;

    public Site getSite() {
        /**
         * 从首页跳转过去
         * 账号：2131189914
         */
        site.addHeader("Referer", "http://weibo.com/u/5679463843/home?topnav=1&wvr=6&mod=logo");
        site.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36");
        return site;
    }

    public void process(Page page) {
//        Html html = page.getHtml();
        List<Map<String, Object>> plist = new ArrayList<Map<String, Object>>();
        Matcher m = pattern.matcher(page.getHtml().toString());
        while (m.find()) {
            String jsonStr = m.group();
//            System.out.println("字符串原型---->    " + jsonStr);
            jsonStr = jsonStr.substring(jsonStr.indexOf("{"), jsonStr.lastIndexOf(")"));
            // 解析json,转换为实体类
            System.out.println("json字符串---->    " + jsonStr);
            jsonStr = UnicodeConverter.toEncodedUnicode(jsonStr, true).replace("\\\\", "\\");
            String str = UnicodeConverter.fromEncodedUnicode(jsonStr.toCharArray(), 0, jsonStr.length());
            System.out.println("最终解码：---->   " + str);
            String htmlString = str.substring(str.indexOf("html\":\"") + 7, str.length() - 2);
            if (htmlString.trim().startsWith("<div class=\"search_feed\">")) {
                document = Jsoup.parse(htmlString);
            }
        }
        List<Element> elements = document.getElementsByAttributeValue("class", "WB_feed_detail clearfix");
        if (elements == null || elements.size() == 0) {
            System.out.println("No more urls to fetch with current keyword.");
            return;
        }
        for (Element elem : elements) {
            Element subEle = elem.getElementsByAttributeValue("class", "name_txt W_fb").first();
            if (subEle != null) {
                Map<String, Object> itemMap = new HashMap<String, Object>();
                String nick_name = subEle.attr("nick-name");
                Attributes attributes = subEle.attributes();
                String usercard = "";
                if (!TextUtils.isEmpty(nick_name)) {
                    itemMap.put("nick_name", nick_name);
                }
                for (Attribute attribute : attributes) {
                    if (attribute.getKey().contains("?refer_flag")) {
                        usercard = attribute.getKey().substring(0, attribute.getKey().length() - "?refer_flag".length());
                    }
                    if (!TextUtils.isEmpty(usercard)) {
                        itemMap.put("usercard", usercard);
                    }
                }
                if (!itemMap.isEmpty()) {
                    plist.add(itemMap);
                }
                System.out.println("用户名： " + nick_name + "\t\t 用户id：  " + usercard);
            }
        }

        page.putField("userInfo", plist);
    }
}